<?php
	namespace app\service\admin;
	use core\BaseService;
	use app\model\Menu;
	use app\model\RoleMenu;
	use app\model\Role;
	use core\Util;
	class MenuService extends BaseService{
		function model(){
			return Menu::$tableName;
		}
		
		function findMenusByParentId($menuFid,$name=null){
			if($menuFid){
				$list = $this->table()
					->where('parent_id','=',$menuFid)
					->orderBy('orders','asc')
					->get();
			}else{
				if($name){
					$list= $this->table()
					->where('name','like','%'.$name.'%')
					->orderBy('orders','asc')
					->take(1)->get();
				}else{
					$list= $this->table()
					->whereNull('parent_id')
					->orderBy('orders','asc')
					->get();
				}
			}
			foreach($list as $index=>$r){
				if($r->is_leaf==0){
					$r->state = 'closed';
				}else{
					$r->state = 'open';
				}
				
			}
			return $list;
		}
		
		function queryAuthMenuByFilter($filter){
			$mTable = $this->table(RoleMenu::$tableName);
			$mTableName = RoleMenu::$tableName;
			$menu = Menu::$tableName;
			$role = Role::$tableName;
			$mTable->join($menu,$mTableName.'.menu_id','=',$menu.'.id')
					->join($role,$mTableName.'.role_id','=',$role.'.id')
					->select($mTableName.'.id as id',$role.'.name as role_name',$menu.'.name as menu_name');
			$this->queryByFilter($filter,$mTable);
		}
		
		function delRoleMenuByFilter($filter){
			$mTable = RoleMenu::$tableName;
			return $this->deleteByFilter($filter,$this->table($mTable));
		}
		
		function autoLoadMunus($menus){
			$allKey = array();
			foreach($menus as $index=>$r){
				$r = json_decode($r);
				$r = Util::obj2arr($r);
				$allKey[] = $r['key'];	
				$first= $this->table()
						->where('key','=',$r['key'])->select('id')->first();
						
				//已存在，则更新菜单
				if($first){
					$upDates = Menu::filter($r);
					$upDates['id'] = $first->id;
					$upDates['parent_id'] = null;
					$this->update($upDates);
				}else{
					//插入新的菜单
					$upDates = Menu::filter($r);
					$this->insert($upDates);
				}
			}
			$this->table()->whereNotIn('key',$allKey)->delete();
			
			//更新菜单 patent_id
			$list = $this->table()->get();
			foreach($list as $index=>$r){
				if(empty($r->parent_id)&&!empty($r->parent_key)){
					$first= $this->table()
						->where('key','=',$r->parent_key)->select('id')->first();
					$upDatas = array(
						'id'=>$r->id,
						'parent_id'=>$first->id
					);
					$this->update($upDatas);
				}
			}
		}
		
		function updateMenuRole($menuIds,$roleIds,$info){
			$menuArr = explode(',',$menuIds);
			$roleArr = explode(',',$roleIds);
			for($i=0;$i<count($menuArr);$i++){
				$mId = $menuArr[$i];
				$insertDatas = array();
				$index = 0;
				for($j=0;$j<count($roleArr);$j++){
					$rId = $roleArr[$j];
					$mTable = $this->table(RoleMenu::$tableName);
					$count = $mTable
						->where('menu_id','=',$mId)
						->where('role_id','=',$rId)->count();
					if($count==0){
						$datas = array(
							'menu_id'=>$mId,
							'role_id'=>$rId
						);
						$insertDatas[$index++] = $datas;
					}
				}
				$mTable = $this->table(RoleMenu::$tableName);
				$mTable->insert($insertDatas);
			}
			$info->success =true;
			$info->msg = '操作成功，菜单授权已更新！';
		}
	}